﻿Imports System.IO

Public Class frmMain

    Dim edmxDoc As Edmx

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        '*
        '* Load the EDMX File
        '*
        edmxDoc = New Edmx()
        edmxDoc.LoadFile("AdventureWorks.edmx")

        '*
        '* Enumerates the Tables / Views
        '*
        With Me.tvStorageEntities.Nodes.Add("tables", "Tables / Views")
            .ImageKey = "TableViewProcedure"
            .SelectedImageKey = "TableViewProcedure"
        End With
        AddTables()

        '*
        '* Enumerates the Procedures / Functions
        '*
        With Me.tvStorageEntities.Nodes.Add("functions", "Stored Procedures")
            .ImageKey = "TableViewProcedure"
            .SelectedImageKey = "TableViewProcedure"
        End With
        AddFuncs()

    End Sub

    Private Sub AddTables()

        For Each type In edmxDoc.RunTime.StorageModels.Schema.EntityTypes
            With Me.tvStorageEntities.Nodes.Item("tables").Nodes.Add(type.Name)
                .ImageKey = "Table"
                .SelectedImageKey = "Table"

                For Each p In type.Properties
                    With .Nodes.Add(p.Name)
                        If (p.IsKey) Then
                            .ImageKey = "PropertyKey"
                            .SelectedImageKey = "PropertyKey"
                        Else
                            .ImageKey = "Property"
                            .SelectedImageKey = "Property"
                        End If
                    End With
                Next
            End With
        Next

    End Sub

    Private Sub AddFuncs()

        For Each func In edmxDoc.RunTime.StorageModels.Schema.Functions
            With Me.tvStorageEntities.Nodes.Item("functions").Nodes.Add(func.Name)
                .ImageKey = "Procedure"
                .SelectedImageKey = "Procedure"

                For Each p In func.Parameters
                    With .Nodes.Add(p.Name)
                        .ImageKey = "Parameter"
                        .SelectedImageKey = "Parameter"
                    End With
                Next
            End With
        Next

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If (Me.dlgSaveEdmx.ShowDialog(Me) = Windows.Forms.DialogResult.Cancel) Then
            Exit Sub
        End If

        edmxDoc.Save(Me.dlgSaveEdmx.FileName)

    End Sub

End Class

